﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using BusinessLayer.RegInfo.Entities;
using System.Data.SqlClient;
using System.Data;


namespace BusinessLayer.RegInfo
{
    public class SubscriptionBL
    {
        private readonly DbHelper _db;

        public SubscriptionBL()
        {
            _db = new DbHelper(new DbConfig().DbRegInfoConnectionString);
        }

        public List<Subscription> GetAll()
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAllSubscriptions) { CommandType = CommandType.StoredProcedure };

            var list = _db.GetList<Subscription>(oCommand);
            return list;
        }
        public int CreateSubscription(Subscription subscript)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.InsertSubscription) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@Code", subscript.Code));
                oCommand.Parameters.Add(new SqlParameter("@Title", subscript.Title));
                oCommand.Parameters.Add(new SqlParameter("@Status", subscript.Status));
                oCommand.Parameters.Add(new SqlParameter("@NumberOfDays", subscript.NumberOfDays));
                oCommand.Parameters.Add(new SqlParameter("@Price", subscript.Price));

                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
        public int UpdateSubscription(Subscription subscript)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.UpdateSubscription) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@SubscriptionID", subscript.SubscriptionID));
                oCommand.Parameters.Add(new SqlParameter("@Code", subscript.Code));
                oCommand.Parameters.Add(new SqlParameter("@Title", subscript.Title));
                oCommand.Parameters.Add(new SqlParameter("@Status", subscript.Status));
                oCommand.Parameters.Add(new SqlParameter("@NumberOfDays", subscript.NumberOfDays));
                oCommand.Parameters.Add(new SqlParameter("@Price", subscript.Price));

                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }

        public Subscription GetByCode(string code)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetSubscriptionByCode) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Code", code));
            var list = _db.GetList<Subscription>(oCommand);
            return list.FirstOrDefault();
        }
        public Subscription GetBySubscriptionID(int id)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetSubscriptionByID) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Id", id));
            var list = _db.GetList<Subscription>(oCommand);
            return list.FirstOrDefault();
        }
        public int DeleteSubscriptionByIds(List<string> lstofids)
        {
            int result = -1;

            string ids = "";
            foreach (var id in lstofids)
            {
                ids += id + ",";
            }
            ids = String.IsNullOrEmpty(ids) ? "" : ids.Substring(0,ids.Length-1);
            if (!String.IsNullOrEmpty(ids))
            {
                try
                {
                    var oCommand = new SqlCommand(String.Format("DELETE FROM Subscriptions WHERE SubscriptionID IN ({0});", ids)) { CommandType = CommandType.Text };
                    result = _db.ExecuteNonQuery(oCommand);
                    return result;
                }
                catch (Exception ex)
                {

                }
            }
            return result;
        }
    }
}
